#include"unp.h"

#define END "\r\n"
#define FTP_PORT 8880

char* program_name;


char* addrName;
char* temp;
char* temp2;
char  htmlPath[100] = "";
char base[100] = "/";
char domain[100] ="";


void set_address(char* hname, char* sname, struct sockaddr_in *sap, char* protocol);
void connectClient(int s, struct sockaddr_in* peer);

void sendMsg(int s, char *msg);
void recvMsg(int s, char *msg);
void printMsg(char *msg);

void recvHtml(int s);
void sendGet(int s);
void downloadFile(int s, char* basePath, char* filename);

int set(char* hname);

int main( int argc, char **argv )
{
	struct sockaddr_in peer;
	struct timeval tp_start, tp_end;
	int msec = 0;
	int s;
	printf("test\n");
	addrName = strtok(argv[1], "/"); //mclab.hufs.ac.kr
	printf("%s\n", addrName);
	temp = strtok(NULL, "/"); //temp = test
	//printf("temp = %s\n", temp);
	strcat(base, temp);
	strcat(base, "/"); //base = /test/
	//printf("base = %s\n", base);
	temp2 = strtok(NULL, "/"); //temp2 = index.html
	//printf("temp2 = %s\n", temp2);

	strcat(htmlPath, base); //htmlPath = /test/
	strcat(htmlPath, temp2); //htmlPath = /test/index.html
	printf("htmlPath = %s\n", htmlPath);

	strcpy(domain, argv[1]);

	gettimeofday(&tp_start, NULL);

	s = set(argv[1]);

	/*
	set_address(argv[1], argv[2], &peer, "tcp");

	s = socket(AF_INET, SOCK_STREAM, 0);
	if(s < 0)
			perror("connect error");
	if(connect(s, (struct sockaddr*)&peer, sizeof(peer)))
		perror("connect failed");
	//connectClient(s, &peer);
	  */

	recvHtml(s);
	sendGet(s);

	gettimeofday(&tp_end, NULL);
	msec = tp_end.tv_sec*1000 + tp_end.tv_usec/1000;
	msec -= tp_start.tv_sec*1000 + tp_start.tv_usec/1000;
	printf("Time : %d milliseconds\n", msec);

	return 0;
}
int set(char* hname)
{
	int sockfd;
	struct sockaddr_in 		servaddr;
	struct hostent *hp;


	sockfd = Socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0)
		perror("connect error");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(80);

	if(hname != NULL)
	{
		strcpy(addrName, hname);
		if(!inet_aton(hname, &servaddr.sin_addr))
		{
			hp = gethostbyname(hname);
			if(hp == NULL)
				perror("unknown host");
			servaddr.sin_addr = *(struct in_addr*)hp->h_addr;
		} //ip from domain name
	} //if input domain
	else
		servaddr.sin_addr.s_addr = htonl(INADDR_ANY);


	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
	return sockfd;

}
void set_address(char* hname, char* sname, struct sockaddr_in *sap, char* protocol)
{
	struct servent *sp;
	struct hostent *hp;
	char *endptr;
	short port;

	bzero(sap, sizeof(*sap)); //initialize
	sap->sin_family = AF_INET;
	if(hname != NULL)
	{
		strcpy(addrName, hname);
		if(!inet_aton(hname, &sap->sin_addr))
		{
			hp = gethostbyname(hname);
			if(hp == NULL)
				perror("unknown host");
			sap->sin_addr = *(struct in_addr*)hp->h_addr;
		} //ip from domain name
	} //if input domain
	else
		sap->sin_addr.s_addr = htonl(INADDR_ANY); //input ip
	port = strtol(sname, &endptr, 0); //string to int
	if(*endptr == '\0')
		sap->sin_port = htons(port); //byte order
	else
	{
		sp = getservbyname(sname, protocol);
		if(sp == NULL)
			perror("unknown service");
		sap->sin_port = sp->s_port;
	}
}
/*
void connectClient(int s, struct sockaddr_in* peer)
{
	char sendBuf[MAXLINE];
	char recvBuf[MAXLINE];

	memset(sendBuf, 0, MAXLINE);
	//sprintf(sendBuf, "GET %s", "/test/index_files/main1.gif\r\n");
	sprintf(sendBuf, "GET %s%s", htmlPath, END);

	sendMsg(sendBuf);
	memset(recvBuf, 0, MAXLINE);
	recvMsg(recvBuf);
	printMsg(recvBuf);
}
*/
void recvHtml(int s)
{
	char sendBuf[MAXLINE];
	char recvBuf[MAXLINE];
	int count;
	int sum = 0;
	int fd;

	memset(sendBuf, 0, MAXLINE);
	sprintf(sendBuf, "GET %s%s", htmlPath, END);
	//sprintf(sendBuf, "GET /test/index.html\r\n""Keep-Alive: 115\r\n""Connection: Keep-Alive\r\n""\r\n");
	sendMsg(s, sendBuf);

	fd = open("html.txt", O_WRONLY | O_CREAT, 0744);

	memset(recvBuf, 0, MAXLINE);
	while((count = Read(s, recvBuf, MAXLINE)) > 0)
	{
		Writen(fd, recvBuf, count);
		memset(recvBuf, 0, MAXLINE);
		sum += count;
	}
	printf("html file total byte : %d\n\n", sum);

	close(fd);
}
void sendGet(int s)
{
	char sendBuf[MAXLINE];
	char recvBuf[MAXLINE];
	char* t1;
	char* t2;
	char* t3;
	char* t4;
	char filename[100] = "";
	char basePath[100] = "";
	FILE* fp;

	//strcpy(basePath, base);
	fp = fopen("html.txt", "r");

	memset(recvBuf, 0, MAXLINE);
	while(fscanf(fp, "%s", recvBuf) == 1)
	{
		if(!strncmp(recvBuf, "src", 3))
		{
			s = set(domain);
			memset(basePath, 0, strlen(basePath));
			memset(filename, 0, strlen(filename));
			strcpy(basePath, base);
			t1 = strtok(recvBuf, "\""); //t1 = "   . "
			t2 = strtok(NULL, "/"); //t2 = index_files
			t3 = strtok(NULL, "."); //t3 = s_02
			t4 = strtok(NULL, "\""); //t4 = JPG

			strcat(basePath, t2);
			strcat(basePath, "/"); //basePath = /test/index_files/
			strcat(basePath, t3);
			strcat(basePath, ".");
			strcat(basePath, t4); //basePath = /test/index_files/s_02.JPG
			//printf("basePath = %s\n", basePath);

			memset(sendBuf, 0, MAXLINE);
			sprintf(sendBuf, "GET %s%s", basePath, END);
			sendMsg(s, sendBuf);

			strcat(filename, t3);
			strcat(filename, ".");
			strcat(filename, t4);

			downloadFile(s, basePath, filename);

		}
	}
}
void downloadFile(int s, char* basePath, char* filename)
{
	char sendBuf[MAXLINE];
	char recvBuf[MAXLINE];
	int count;
	int sum = 0;
	int fd;

	memset(sendBuf, 0, MAXLINE);
	sprintf(sendBuf, "GET %s%s", basePath, END);
	sendMsg(s, sendBuf);
	sendBuf[strlen(sendBuf)-1] = '\0';
	printf("sendBuf = %s", sendBuf);

	fd = open(filename, O_WRONLY | O_CREAT, 0744);

	memset(recvBuf, 0, MAXLINE);

	while((count = Read(s, recvBuf, MAXLINE)) > 0)
	{
		Writen(fd, recvBuf, count);
		memset(recvBuf, 0, MAXLINE);
		sum += count;
	}
	printf("file total byte : %d\n\n", sum);
	close(fd);
}
void sendMsg(int s, char *msg)
{
	Writen(s, msg, strlen(msg));
}
void recvMsg(int s, char *msg)
{
	char recvBuf[4096];
	memset(recvBuf, 0, MAXLINE);

	while(Read(s, recvBuf, MAXLINE)>0)
	{
		memset(recvBuf, 0, MAXLINE);
		printMsg(recvBuf);
	}

//	Readline(s, msg, MAXLINE);
//	msg[strlen(msg)] = '\0';
}
void printMsg(char *msg)
{
	printf("%s", msg);
}
